#include "Camera.h"



Camera::Camera(Renderer* renderer)
{
   setRenderer( renderer );
   
   m_position.set( 0.0, 0.0, 0.0 );
   m_viewDir.set( 0.0, 0.0, -1.0 );
   m_rightVector.set( 1.0, 0.0, 0.0 );
   m_upVector.set( 0.0, 1.0, 0.0 );
   
   m_rotatedX = m_rotatedX = m_rotatedZ = 0.0f;
}


void Camera::setRenderer(Renderer* renderer)
{
   m_renderer = renderer;
}

Renderer* Camera::renderer()
{
   return m_renderer;
}


void Camera::update()
{
   gtypes::Vector3 viewPoint = m_position + m_viewDir;
   m_renderer->lookAt( m_position, viewPoint, m_upVector );
}


void Camera::rotateX( float angle )
{
    m_rotatedX += angle;

    m_viewDir = m_viewDir * cos( angle * PI / 180)
              + m_upVector * sin( angle * PI / 180 );
    m_viewDir.normalise();

    m_upVector.cross(m_viewDir,m_rightVector);
    m_upVector *= -1;

    /*
    RotatedX += Angle;

        //Rotate viewdir around the right vector:
    ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
            + UpVector*sin(Angle*PIdiv180));

        //now compute the new UpVector (by cross product)
    UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
    */
}

void Camera::rotateY( float angle )
{
    m_rotatedY += angle;

    m_viewDir = m_viewDir * cos( angle * PI / 180)
              - m_rightVector * sin( angle * PI / 180 );
    m_viewDir.normalise();

    m_rightVector.cross(m_viewDir,m_upVector);

    /*
    RotatedY += Angle;

        //Rotate viewdir around the up vector:
    ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
            - RightVector*sin(Angle*PIdiv180));

        //now compute the new RightVector (by cross product)
    RightVector = CrossProduct(&ViewDir, &UpVector);
    */
}

void Camera::rotateZ( float angle )
{
    m_rotatedZ += angle;

    m_rightVector = m_rightVector * cos( angle * PI / 180)
                  + m_upVector * sin( angle * PI / 180 );
    m_rightVector.normalise();

    m_upVector.cross(m_viewDir,m_rightVector);
    m_upVector *= -1;

    /*
    RotatedZ += Angle;

        //Rotate viewdir around the right vector:
    RightVector = Normalize3dVector(RightVector*cos(Angle*PIdiv180)
            + UpVector*sin(Angle*PIdiv180));

        //now compute the new UpVector (by cross product)
    UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
    */
}

void Camera::moveForward( float distance )
{
    m_position += ( m_viewDir * -distance );
}

void Camera::moveUpward( float distance )
{
    m_position += ( m_upVector * distance );
}

void Camera::strafeRight( float distance )
{
    m_position += ( m_rightVector * distance );
}
